// 获取应用实例
const app = getApp();

const z = require("../../../utils/util");
const api = require("../../../utils/api");

// 初始化数据库
const db = wx.cloud.database();
const uimg = db.collection("uimg");
const message = db.collection("message");

// pages/edit/edit.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    theme: 1,
    switch: false,
    openid: "",
    o_openid: "",
    fileList: [],
    userVal: "",
    emailVal: "",
    infoVal: "",
    imgUrl: "",
    love_word: [],
    fileID: '',
    userInfoMask: false,

    navHeight: app.globalData.navBarHeight,
    menuHeight: app.globalData.menuHeight,
    menuRight: app.globalData.menuRight,
    menuTop: app.globalData.menuTop,
  },

  userInput(e) {
    this.setData({
      userVal: e.detail.value,
    });
  },

  infoInput(e) {
    this.setData({
      infoVal: e.detail.value,
    });
  },

  getUserInfo() {
    const that = this;
    const { openid } = that.data;
    uimg.where({ _openid: openid }).get({
      success: res => {
        const { avatarUrl, nickname, o_openid, love_word, fileID } = res.data[0];
        that.setData({
          userimg: avatarUrl,
          userVal: nickname,
          o_openid,
          love_word,
          fileID
        })
      }
    })
  },

  // 上传图片
  afterRead(e) {
    const that = this;
    const { file } = e.detail;
    const { url } = e.detail.file;
    that.setData({
      fileList: [file],
      imgUrl: url,
    });
    // wx.setStorageSync("xcx-loginImg", url);
    wx.setStorageSync("xcx-fileList", [file]);
  },

  delete() {
    this.setData({
      fileList: [],
    });
    wx.setStorageSync("xcx-fileList", []);
  },

  // 保存
  save() {
    const that = this;
    let { userVal, emailVal, infoVal, imgUrl, openid, o_openid, love_word, fileID } = this.data;
    if (imgUrl == "") {
      console.log("没有修改图像");
      uimg.where({ _openid: openid }).update({
        data: {
          nickname: userVal,
        },
      });
      message.where({ _openid: openid }).update({
        data: {
          nickname: userVal,
        },
      });
      wx.navigateBack();
      wx.showToast({
        title: "修改成功",
        icon: "success",
      });
    } else {
      z.uploadFile(
        () => {
          wx.showLoading({
            title: "修改中",
          });
          that.setData({
            disabled: true,
          });
        },
        imgUrl,
        "avatarUrl",
        (url, fileId) => {
          // console.log(url);
          uimg.where({ _openid: openid }).update({
            data: {
              avatarUrl: url,
              nickname: userVal,
              fileID: fileId
            },
            success: () => {
              wx.navigateBack();

              wx.showToast({
                title: "修改成功",
                icon: "success",
              });
            },
            fail: () => {
              wx.showToast({
                title: "修改失败",
                icon: "error",
              });
            },
            complete: () => {
              wx.hideLoading();
            },
          });
          message.where({ _openid: openid }).update({
            data: {
              avatarUrl: url,
              nickname: userVal,
            },
          });

          if (o_openid) {
            love_word.forEach(i => {
              if (i.openid == openid) {
                i.avatarUrl = url;
                i.nickname = userVal;
              }
            });
            uimg.where({ _openid: openid }).update({
              data: {
                love_word
              },
              success: res => {
                uimg.where({ _openid: o_openid }).update({
                  data: {
                    love_word
                  },
                })
              }
            });
          }
          // console.log(fileID);
          // console.log(that.data.fileID);
          api.delCloudFile(fileID);

          wx.setStorageSync("avatarUrl", url);
        },
        () => {
          wx.hideLoading();
          wx.showToast({
            title: "修改失败",
            icon: "error",
          });
        }
      );
    }

    wx.setStorageSync("nickname", userVal);
    wx.setStorageSync("xcx-userEmail", emailVal);
    wx.setStorageSync("xcx-zoneIntro", infoVal);
  },

  authWechat() {
    this.setData({
      userInfoMask: true,
    });
  },

  closeUserInfoMask(e) {
    this.setData({
      userInfoMask: false,
    });
  },

  setUser(e) {
    wx.navigateBack();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) { },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const openid = wx.getStorageSync("openid");
    const o_openid = wx.getStorageSync("o_openid");
    this.setData({
      theme: app.globalData.theme,
      switch: app.globalData.switch,
      openid,
      o_openid,
      userVal: wx.getStorageSync("nickname") || "微信用户",
      emailVal: wx.getStorageSync("xcx-userEmail") || "",
      infoVal: wx.getStorageSync("xcx-zoneIntro") || "这是我们的小家",
      // fileList: wx.getStorageSync("xcx-fileList") || [],
    });

    this.getUserInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
